package com.smallrig.mall.template.builder;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class Builder<T> {

    private final Supplier<T> constructor;

    private final List<Consumer<T>> dInjects = new ArrayList<>();

    private Builder(Supplier<T> constructor){
        this.constructor = constructor;
    }

    public static <T> Builder<T> builder(Supplier<T> constructor){
        return new Builder<>(constructor);
    }


    public <Param> Builder<T> with(BiConsumer<T,Param> consumer,Param p){
        Consumer<T> c = instance -> consumer.accept(instance,p);
        dInjects.add(c);
        return this;
    }

    public T build(){
        T instance = constructor.get();
        dInjects.forEach(dInject->dInject.accept(instance));
        return instance;
    }

}
